package com.neuedu.wemall.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.wemall.common.vo.ResponseData;
import com.neuedu.wemall.dao.entity.Address;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.neuedu.wemall.service.IAddressService;

import java.util.List;

/**
 * Author lu
 * Date   2025-09-08 15:17:25
 */
@Log4j2
@RequestMapping("/wemall/member/address/api")
@RestController
@Tag(name = "收件地址控制器")
public class AddressController extends BaseController {

    @Autowired
    private IAddressService addressService;


    /**
     * 创建收件地址
     *
     * @param address 收件地址对象
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建收件地址")
    @Parameters({
            @Parameter(name = "token", description = "令牌", required = true),
            @Parameter(name = "recvName", description = "收件人姓名", required = true),
            @Parameter(name = "recvMobile", description = "收件人手机号码", required = true),
            @Parameter(name = "province", description = "省份", required = true),
            @Parameter(name = "city", description = "地级市", required = true),
            @Parameter(name = "district", description = "区", required = true),
            @Parameter(name = "street", description = "街道", required = true),
            @Parameter(name = "detail", description = "详细地址", required = true),
            @Parameter(name = "isDefault", description = "是否为默认地址(1:是;2:否)", required = true)
    })
    public ResponseData createAddress(@Parameter(hidden = true) Address address) {
        //会员用户ID
        int userId = getMemberUserID();
        address.setUserId(userId);
        return addressService.createAddress(address);
    }

    /**
     * 删除收件地址
     *
     * @param addressId 收件地址ID
     * @return
     */
    @Operation(summary = "删除邮件地址")
    @DeleteMapping("/remove/{addressId}")
    @Parameters({
            @Parameter(name = "token", description = "令牌", required = true)
            , @Parameter(name = "addressId", description = "地址ID", required = true)
    })
    public ResponseData removeAddress(@PathVariable("addressId") @Parameter(hidden = false) int addressId) {
        //会员用户ID
        int userId = getMemberUserID();
        return addressService.deleteAddress(addressId, userId);
    }

    /**
     * 修改地址
     *
     * @param address 修改后地址对象
     * @return
     */
    @PostMapping("/modify_by_id")
    @Operation(summary = "修改收件地址")
    @Parameters({
            @Parameter(name = "token", description = "令牌", required = true),
            @Parameter(name = "id", description = "地址ID", required = true),
            @Parameter(name = "recvName", description = "收件人姓名", required = false),
            @Parameter(name = "recvMobile", description = "收件人手机号码", required = false),
            @Parameter(name = "province", description = "省份", required = false),
            @Parameter(name = "city", description = "地级市", required = false),
            @Parameter(name = "district", description = "区", required = false),
            @Parameter(name = "street", description = "街道", required = false),
            @Parameter(name = "detail", description = "详细地址", required = false),
            @Parameter(name = "isDefault", description = "是否为默认地址(1:是;2:否)", required = false)
    })
    public ResponseData modifyAddress(@Parameter(hidden = true) Address address) {
        int userId = getMemberUserID();
        address.setUserId(userId);
        return addressService.modifyAddress(address);
    }

    /**
     * 查询会员所有收件地址
     *
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "查询会员所有收件地址")
    @Parameters({
            @Parameter(name = "token", description = "令牌", required = true)
    })
    public ResponseData list() {
        //会员用户ID
        int userId = getMemberUserID();
        LambdaQueryWrapper<Address> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Address::getUserId, userId);
        lqw.orderByAsc(Address::getIsDefault);
        List<Address> addressList = addressService.list(lqw);
        return ResponseData.success(addressList);
    }
}